package com.example.tests;

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

import com.example.framework.AccountHelper;
import com.example.framework.AdminHelper;
import com.example.framework.JamesHelper;
import com.example.framework.User;

public class SignUpTests extends TestBase {

	public User user = new User()
		.setLogin("testuser")
		.setPassword("password")		
		.setEmail("testuser@localhost.localdomain");

	private JamesHelper james;
	private AdminHelper admin;
	private AccountHelper accHelper;

	@BeforeClass
	public void createMailUser() {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		if (!james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}
	}

	@Test
	public void newUserShouldSignUp() throws Exception {
		//admin.deleteUserIfExists(user);
		accHelper.signUp(user);
		accHelper.logIn(user);
		assertThat(accHelper.loggedIn(), equalTo(user.login));
	}

	@Test
	public void existingUserShouldNotSignUp() {
		try {
			accHelper.signUp(user);
		} catch (Exception e) {
			assertThat(e.getMessage(),
					containsString("That username is already being used"));
			return;
		}
		Assert.fail("Exception expected. User is created!");
	}

	@AfterClass
	public void deleteMailUser() {
		if (james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
		}
	}

}
